package com.culture.config.common.util;

import java.util.Collection;
import java.util.Objects;
import java.util.Optional;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.culture.config.common.model.BusinessException;

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@UtilityClass
public class ExceptionUtil {

	public static void businessExceptionWhenBlank(String cs, String message){		
		ExceptionUtil.businessExceptionWhenTrue(StringUtils.isBlank(cs), message);
	}	
	
	public static <T> void businessExceptionWhenEmpty(Collection<T> collections, String message){		
		ExceptionUtil.businessExceptionWhenTrue(CollectionUtils.isEmpty(collections), message);
	}	
	
	public static <T> void businessExceptionWhenNull(T obj, String message){		
		ExceptionUtil.businessExceptionWhenTrue(Objects.isNull(obj), message);
	}	
	
	public static void businessExceptionWhenTrue(boolean flag, String message){
		if(flag) {
			ExceptionUtil.businessException(message);
		}
	}
	
	public static void businessException(String message){		
		log.error(message);
		throw new BusinessException(Optional.ofNullable(message).orElse("System exception"));
	}
}
